//
//  ViewController.m
//  Memo
//
//  Created by A-01 on 2020/7/8.
//  Copyright © 2020 A-01. All rights reserved.
//

#import "ViewController.h"
#import "WishListViewController.h"
#import "AddWishViewController.h"
#import "IntroduceViewController.h"

@interface ViewController ()

@property (nonatomic, weak)UIButton * rightButton;
@property (nonatomic, weak)UIView * whiteView;
@property (nonatomic, weak)UIImageView * logoImageView;
@property (nonatomic, strong)UIButton * listButton;
@property (nonatomic, strong)UIButton * addButton;
@property (nonatomic, strong)UIButton * processButton;
@property (nonatomic, strong)UIButton * completedButton;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.backButton.hidden = YES;
    [self setupUI];
}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:YES];
    
    NSArray * arr = DEF_GET_OBJECT(@"data");
    if (arr.count > 0) {
        
        CGFloat Start_X = 30.0f;      // 第一个按钮的X坐标
        CGFloat Start_Y = 20.0f;     // 第一个按钮的Y坐标
        CGFloat Width_Space = (kScreenW - 70 - 82 * 3) / 2;      // 2个按钮之间的横间距
        CGFloat Height_Space = 20.0f;     // 竖间距
        CGFloat Button_Height = 96.0f;    // 高
        CGFloat Button_Width = 82.0f;    // 宽
        NSUInteger count = arr.count;
        if (arr.count > 6) {
            
            count = 6;
        }
        for (int i = 0 ; i < count; i++) {
            
            NSInteger index = i % 3;
            NSInteger page = i / 3;
                 
            // 圆角按钮
            UIButton * imageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            imageBtn.frame = CGRectMake(index * (Button_Width + Width_Space) + Start_X, page  * (Button_Height + Height_Space)+Start_Y, Button_Width, Button_Height);
            imageBtn.tag = i;
            if (i%2==0) {
                //如果是偶数
                    [imageBtn setImage:[UIImage imageNamed:@"memo_0"] forState:UIControlStateNormal];
                }else{
                    //如果是奇数
                    [imageBtn setImage:[UIImage imageNamed:@"memo_1"] forState:UIControlStateNormal];
                }
            [self.whiteView addSubview:imageBtn];
            //按钮点击方法
            [imageBtn addTarget:self action:@selector(imageBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        }
        
        self.logoImageView.hidden = YES;
    }else{
        
        self.logoImageView.hidden = NO;
    }
}


//点击按钮方法,这里容易犯错
-(void)imageBtnClick:(UIButton *)sender{
    //记住,这里不能写成"mapBtn.tag",这样你点击任何一个button,都只能获取到最后一个button的值,因为前边的按钮都被最后一个button给覆盖了
    NSArray * arr = DEF_GET_OBJECT(@"data");
    NSDictionary * dict = arr[sender.tag];
    
    AddWishViewController * addVC = [AddWishViewController new];
    
    addVC.titleStr = @"detail";
    addVC.dict = [NSMutableDictionary dictionaryWithDictionary:dict];
    
    [self.navigationController pushViewController:addVC animated:YES];
}


- (void)setupUI{
    
    UIButton * rightButton = [[UIButton alloc] init];
    
    [rightButton setImage:[UIImage imageNamed:@"感叹号"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(clickRightButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.rightButton = rightButton;
    
    [self.view addSubview:self.rightButton];
    
    
    UIView * whiteView = [UIView new];
    
    whiteView.backgroundColor = ZRWhiteColor;
    whiteView.userInteractionEnabled = YES;
    whiteView.layer.cornerRadius = 10;
    whiteView.layer.masksToBounds = YES;
    
    self.whiteView = whiteView;
    
    [self.view addSubview:self.whiteView];
    
    
    UIImageView * logoImageView = [UIImageView new];
    
    logoImageView.image = [UIImage imageNamed:@"logo"];
    logoImageView.contentMode = UIViewContentModeScaleAspectFill;
    
    self.logoImageView = logoImageView;
    
    [self.whiteView addSubview:self.logoImageView];
    
    self.listButton = [self setupButtonWithTitle:@"Wish list"];
    [self.view addSubview:self.listButton];
    self.addButton = [self setupButtonWithTitle:@"Add a wish"];
    [self.view addSubview:self.addButton];
    self.processButton = [self setupButtonWithTitle:@"Processing"];
    [self.view addSubview:self.processButton];
    self.completedButton = [self setupButtonWithTitle:@"Completed"];
    [self.view addSubview:self.completedButton];
}


- (void)clickRightButton{
    
    IntroduceViewController * introVC = [IntroduceViewController new];
    
    [self.navigationController pushViewController:introVC animated:YES];
}


- (void)clickBottomButton:(UIButton *)button{
    
    if (button == self.listButton) {
        
        WishListViewController * listVc = [WishListViewController new];
        listVc.type = 1;
        [self.navigationController pushViewController:listVc animated:YES];
    }else if (button == self.addButton){
        
        AddWishViewController * addVC = [AddWishViewController new];
        addVC.titleStr = @"Add a Wish";
        
        [self.navigationController pushViewController:addVC animated:YES];
    }else if (button == self.processButton){
        
        WishListViewController * listVc = [WishListViewController new];
        listVc.type = 2;
        [self.navigationController pushViewController:listVc animated:YES];
    }else{
        
        WishListViewController * listVc = [WishListViewController new];
        listVc.type = 3;
        [self.navigationController pushViewController:listVc animated:YES];
    }
    
}



-(void)viewDidLayoutSubviews{
    
    [super viewDidLayoutSubviews];
    
    [self.rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.view).offset(-15);
        make.top.mas_equalTo(self.view).offset(Height_StatusBar + 15);
        make.height.width.mas_equalTo(37);
    }];
    
    [self.whiteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(5);
        make.trailing.mas_equalTo(self.view).offset(-5);
        make.top.mas_equalTo(self.rightButton.mas_bottom).offset(20);
        make.height.mas_equalTo(260);
    }];
    
    [self.logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.whiteView);
        make.leading.trailing.mas_equalTo(self.whiteView);
    }];
    
    [self.listButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(kScreenW / 3 / 4);
        make.top.mas_equalTo(self.whiteView.mas_bottom).offset(45);
        make.width.mas_equalTo(kScreenW / 3);
        make.height.mas_equalTo(kScreenW / 3 * 0.3);
    }];
    
    [self.addButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.width.height.mas_equalTo(self.listButton);
        make.trailing.mas_equalTo(self.view).offset(-kScreenW / 3 / 4);
    }];
    
    [self.processButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.width.height.mas_equalTo(self.listButton);
        make.top.mas_equalTo(self.listButton.mas_bottom).offset(15);
    }];
    
    [self.completedButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.width.height.mas_equalTo(self.processButton);
        make.trailing.mas_equalTo(self.addButton);
    }];
}


- (UIButton *)setupButtonWithTitle:(NSString *)title{
    
    UIButton * button = [UIButton new];
    
    [button setBackgroundImage:[UIImage imageNamed:@"矩形"] forState:UIControlStateNormal];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:ZRBlackColor forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clickBottomButton:) forControlEvents:UIControlEventTouchUpInside];
    button.titleLabel.font = [UIFont systemFontOfSize:17];
    
    return button;
}


@end
